const app = getApp()
Page({

	data: {
		music: {
			url: "https://webfs.yun.kugou.com/202011151702/9868cc5df316ec29216736ec6cd51cd3/part/0/961159/G201/M01/0D/05/qZQEAF5b9B6AagcNADv3xPLXKTo415.mp3",
			title: "那些你很冒险的梦-林俊杰",
			coverImg: "https://p3fx.kgimg.com/stdmusic/20150716/20150716004147248755.jpg"
		},
		isPlay:false,
		_mgr:''
	},

	onLoad: function (options) {
		console.log(111)
		this.getStorage()
		const mgr = wx.getBackgroundAudioManager()
		this.data._mgr = mgr
		mgr.onPlay(this.onMusicStart)
		mgr.onPause(this.onMusicStop)
		this.setData({
			isPlay:app.gIsPlayingMusic
		})
	},
	onCollection() {
		let collected = wx.getStorageSync('collected')
		if (collected) {
			//async、await 写法
			// const status = await wx.showModal({
			// 	title:'您确定取消收藏吗？'
			// })
			// Promise写法
			// status.then((res) => {
			// 	if(res.confirm){
			// 		wx.setStorageSync('collected',false)
			// 		this.setData({
			// 			flag:false
			// 		})
			// 	}
			// })
			//还有利用success回调函数
			wx.setStorageSync('collected', false)
			this.setData({
				flag: false
			})
			wx.showToast({
				title: '取消成功！',
			})

		} else {
			wx.setStorageSync('collected', true)
			this.setData({
				flag: true
			})
			wx.showToast({
				title: '收藏成功！',
			})
		}
	},
	getStorage() {
		const flag = wx.getStorageSync('collected')
		this.setData({
			flag,
		})
	},
	async onShare() {
		const result = await wx.showActionSheet({
			itemList: ['分享到QQ', '分享到微信', '分享到朋友圈'],
			// success (res) {
			// 	console.log(res)
			// }
		})
	},
	onMusicStart(event) {
		const mgr = this.data._mgr
		mgr.src = this.data.music.url
		mgr.title = this.data.music.title
		mgr.coverImgUrl = this.data.music.coverImg
		app.gIsPlayingMusic = true
		this.setData({
			isPlay:true
		})
	},
	onMusicStop (event) {
		const mgr = this.data._mgr
		app.gIsPlayingMusic = false
		mgr.pause()
		this.setData({
			isPlay: false
		})
	}
})